<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class buy_uqcms extends control
{
    function __construct()
    {
        $this->uid = @$_SESSION['user']['uid'];
        parent::__construct();
    }
    function pay_trade()
    {
        $oid = isset($_GET['oid']) ? $_GET['oid'] : showmsg('提交错误');
        $order_row = $this->db->get_one("select * from " . table('order_info') . " where order_no = '" . $oid . "'");
        if ($order_row['pay_time'] == '0') {
            $row = $this->db->get_one("select * from " . table('admin_pay') . " where uid = " . $this->uid . " and settle_type = 0 and order_no = '" . $order_row['order_no'] . "'");
            if (!$row['id']) {
                if (isset($row['trade_no'])) {
                    $trade_no = $row['trade_no'];
                } else {
                    $build_trade_no = build_order_no();
                    $setarr['uid'] = $this->uid;
                    $setarr['pay_type'] = '1';
                    $setarr['settle_type'] = '0';
                    $setarr['trade_no'] = $build_trade_no;
                    $setarr['order_no'] = $order_row['order_no'];
                    $setarr['pay_price'] = $order_row['total_price'];
                    $setarr['pay_status'] = '0';
                    $setarr['ip'] = get_ip();
                    $setarr['addtime'] = time();
                    $query = $this->db->add(table('admin_pay'), $setarr);
                    if ($query) {
                        $trade_no = $build_trade_no;
                    } else {
                        exit('提交错误');
                    }
                }
                $link_jump = cfg('browser');
                if ($link_jump == 'pc') {
                    header("Location:member.php?m=account&a=pay&trade_no=" . $trade_no);
                } else {
                    header("Location:wap.php?m=account&a=pay&trade_no=" . $trade_no);
                }
            } else {
                $link_jump = cfg('browser');
                if ($link_jump == 'pc') {
                    header("Location:member.php?m=account&a=pay&trade_no=" . $row['trade_no']);
                } else {
                    header("Location:wap.php?m=account&a=pay&trade_no=" . $row['trade_no']);
                }
            }
        } else {
            exit('已经支付');
        }
    }
    public function del_order()
    {
        $oid = isset($_GET['oid']) ? $_GET['oid'] : showmsg('ID ERROR');
        $row = $this->db->get_one("select * from " . table('order_info') . " where uid = " . $this->uid . " and order_no = " . $oid);
        if ($row['order_status'] == '0' || $row['order_status'] == '4' || $row['order_status'] == '5') {
            if ($row['del_shop'] == '1') {
                $this->db->autocommit();
                $q1 = $this->db->delete(table('order_info'), 'id = ' . $row['id']);
                $q2 = $this->db->delete(table('order_goods'), 'oid = ' . $row['id']);
                $q3 = $this->db->delete(table('order_address'), 'oid = ' . $row['id']);
                $q4 = $this->db->delete(table('order_score'), 'oid = ' . $row['id']);
                if ($q1 && $q2 && $q3 && $q4) {
                    $this->db->commit();
                    showmsg('删除成功');
                } else {
                    $this->db->rollback();
                    showmsg('删除失败');
                }
            } else {
                $query = $this->db->update(table('order_info'), array('del_user' => '1'), 'uid = ' . $this->uid . ' and id = ' . $row['id']);
                if ($query) {
                    showmsg('删除成功');
                } else {
                    showmsg('删除失败');
                }
            }
        } else {
            showmsg('订单正在交易当中，无法删除');
        }
    }
    function cancel_order_operate()
    {
        $oid = isset($_POST['oid']) ? $_POST['oid'] : error_json('订单号不能为空');
        $reason = isset($_POST['reason']) ? $_POST['reason'] : error_json('请选择退款原因');
        $result = $this->module('order')->cencel_order($oid, $reason);
        if ($result['error'] == '0') {
            right_json();
        } else {
            error_json($result['msg']);
        }
    }
    function receive()
    {
        $oid = isset($_POST['oid']) ? $_POST['oid'] : error_json('提交错误');
        $result = $this->module('order')->receive_goods($oid);
        if ($result) {
            right_json();
        } else {
            error_json($result['msg']);
        }
    }
    public function comment_save()
    {
        if ($_POST) {
            $oid = isset($_POST['oid']) ? $_POST['oid'] : error_json('订单提交错误');
            $row = $this->db->get_one("select id,shop_id,order_no from " . table('order_info') . " where uid = " . $this->uid . " and order_no = '" . $oid . "'");
            if ($row['id']) {
                $this->db->autocommit();
                $shop_goods_score = isset($_POST['shop_goods_score']) ? $_POST['shop_goods_score'] : '5';
                $shop_service_score = isset($_POST['shop_service_score']) ? $_POST['shop_service_score'] : '5';
                $shop_logistics_score = isset($_POST['shop_logistics_score']) ? $_POST['shop_logistics_score'] : '5';
                $os_arr['uid'] = $this->uid;
                $os_arr['spid'] = $row['shop_id'];
                $os_arr['oid'] = $row['id'];
                $os_arr['goods'] = $shop_goods_score;
                $os_arr['service'] = $shop_service_score;
                $os_arr['logistics'] = $shop_logistics_score;
                $os_arr['addtime'] = time();
                $query = $this->db->add(table('order_score'), $os_arr);
                $shop_row = $this->db->get_one("select * from " . table('shop_score') . " where id = " . $row['shop_id']);
                if ($shop_row) {
                    if ($shop_row['id']) {
                        $goods_scores = $shop_row['goods_score'] + $shop_goods_score;
                        $goods_num = $shop_row['goods_num'] + 1;
                        $sp_score['goods'] = price_format($goods_scores / $goods_num, 1);
                        $sp_score['goods_score'] = $goods_scores;
                        $sp_score['goods_num'] = $goods_num;
                        $service_scores = $shop_row['service_score'] + $shop_service_score;
                        $service_num = $shop_row['service_num'] + 1;
                        $sp_score['service'] = price_format($service_scores / $service_num, 1);
                        $sp_score['service_score'] = $service_scores;
                        $sp_score['service_num'] = $service_num;
                        $logistics_scores = $shop_row['logistics_score'] + $shop_logistics_score;
                        $logistics_num = $shop_row['logistics_num'] + 1;
                        $sp_score['logistics'] = price_format($logistics_scores / $logistics_num, 1);
                        $sp_score['logistics_score'] = $logistics_scores;
                        $sp_score['logistics_num'] = $logistics_num;
                        $query2 = $this->db->update(table('shop_score'), $sp_score, 'shop_id = ' . $row['shop_id']);
                    } else {
                        $sp_score = array('shop_id' => $shop_id, 'goods' => $goods_score, 'goods_score' => $goods_score, 'goods_num' => '1', 'service' => $service_score, 'service_score' => $service_score, 'service_num' => '1', 'logistics' => $logistics_score, 'logistics_score' => $logistics_score, 'logistics_num' => '1');
                        $query2 = $this->db->add(table('shop_score'), $sp_score);
                    }
                }
                $goods_id = isset($_POST['goods_id']) ? $_POST['goods_id'] : error_json('提交产品编号错误');
                $goods_score = isset($_POST['goods_score']) ? $_POST['goods_score'] : error_json('产品评分不能为空');
                $goods_content = isset($_POST['goods_content']) ? $_POST['goods_content'] : error_json('评价内容不能为空');
                $goods_comment_img = isset($_POST['goods_comment_img']) ? $_POST['goods_comment_img'] : '';
                $count = 0;
                $loop = 0;
                $loop_ci = 0;
                $ogall = $this->db->get_all("select id,oid,gid,shop_id,comment_time from " . table('order_goods') . " where oid = " . $row['id']);
                foreach ($ogall as $k => $v) {
                    $ogid = $v['id'];
                    if ($goods_score[$ogid] && $goods_content[$ogid]) {
                        if ($v['comment_time'] == '0') {
                            $status_query = $this->db->update(table('order_goods'), array('comment_time' => time(), 'order_status' => '5'), 'id = ' . $v['id']);
                            $data['uid'] = $this->uid;
                            $data['shop_id'] = $v['shop_id'];
                            $data['gid'] = $goods_id[$ogid];
                            $data['oid'] = $v['oid'];
                            $data['ogid'] = $v['id'];
                            $data['score'] = $goods_score[$ogid];
                            $data['content'] = $goods_content[$ogid];
                            $data['addtime'] = time();
                            $comment_query = $this->db->add(table('goods_comment'), $data);
                            $comment_id = $this->db->insert_id();
                            if (isset($goods_comment_img[$ogid])) {
                                foreach ($goods_comment_img[$ogid] as $vv) {
                                    $img_data['gid'] = $goods_id[$ogid];
                                    $img_data['comment_id'] = $comment_id;
                                    $img_data['images'] = $vv;
                                    $this->db->add(table('goods_comment_img'), $img_data);
                                }
                            }
                            if ($goods_score[$ogid] >= 4) {
                                $set_sc = 'score_good = score_good+1 ';
                            } elseif ($goods_score[$ogid] >= 2) {
                                $set_sc = 'score_average = score_average+1 ';
                            } else {
                                $set_sc = 'score_bad = score_bad+1 ';
                            }
                            $goods_comment_query = $this->db->query("update " . table('goods') . " SET comment=comment+1," . $set_sc . " WHERE id = " . $goods_id[$ogid] . " LIMIT 1;");
                            if ($status_query && $comment_query && $goods_comment_query) {
                                $count++;
                            }
                            $loop++;
                            $loop_ci--;
                        }
                    }
                    if ($v['comment_time'] == '0') {
                        $loop_ci++;
                    }
                }
                if ($loop_ci == 0) {
                    $order_query = $this->db->update(table('order_info'), array('comment_time' => time(), 'order_status' => '5'), 'id = ' . $row['id']);
                    if ($query && $query2 && $order_query && $count == $loop) {
                        $this->db->commit();
                        $result['error'] = '0';
                        echo json_encode($result);
                    } else {
                        $this->db->rollback();
                        error_json('评价失败');
                    }
                } else {
                    if ($query && $query2 && $count == $loop) {
                        $this->db->commit();
                        $result['error'] = '0';
                        echo json_encode($result);
                    } else {
                        $this->db->rollback();
                        error_json('评价失败');
                    }
                }
            } else {
                error_json('提交的订单不存在');
            }
        } else {
            error_json('提交错误');
        }
    }
    function refund_apply()
    {
        if ($_POST) {
            $service_id = isset($_POST['service_id']) ? $_POST['service_id'] : error_json('提交错误：SERVICE ID NULL');
            $reason = isset($_POST['reason']) ? $_POST['reason'] : '';
            $price = isset($_POST['price']) ? $_POST['price'] : error_json('退款金额不能为空');
            $remarks = isset($_POST['remarks']) ? $_POST['remarks'] : '';
            $refund_img = isset($_POST['refund_img']) ? $_POST['refund_img'] : '';
            $ogrow = $this->db->get_one("select * from " . table('order_goods') . " where uid = " . $this->uid . " and service_id = '" . $service_id . "'");
            $refund_row = $this->db->get_one("select id,service_id from " . table('refund') . " where service_id = '" . $service_id . "' limit 1");
            if ($ogrow['id'] && !$refund_row['id']) {
                if ($ogrow['price'] * $ogrow['buy_num'] >= $price) {
                    $this->db->autocommit();
                    $setsql['uid'] = $this->uid;
                    $setsql['shop_id'] = $ogrow['shop_id'];
                    $setsql['goods_id'] = $ogrow['gid'];
                    $setsql['service_id'] = $ogrow['service_id'];
                    $setsql['reason'] = $reason;
                    $setsql['price'] = $price;
                    $setsql['remarks'] = $remarks;
                    $setsql['addtime'] = time();
                    $query = $this->db->add(table('refund'), $setsql);
                    $insert_id = $this->db->insert_id();
                    if ($query) {
                        if ($refund_img) {
                            foreach ($refund_img as $v) {
                                $img_arr[] = $v;
                            }
                            $log_arr['images'] = serialize($img_arr);
                        }
                        $refund_type = $this->module('refund')->refund_reason($reason);
                        $log_arr['rid'] = $insert_id;
                        $log_arr['title'] = '买家：申请退货';
                        $log_arr['content'] = '退款类型：' . $refund_type . '<br>退款金额：' . $price . '<br>备注：' . $remarks;
                        $exec = $this->module('refund')->add_process($log_arr);
                        if ($exec) {
                            $this->db->commit();
                            $data['error'] = '0';
                            echo json_encode($data);
                        } else {
                            $this->db->rollback();
                            error_json('申请失败');
                        }
                    } else {
                        $this->db->rollback();
                        error_json('申请失败');
                    }
                } else {
                    error_json('申请的金额不能超过购买金额');
                }
            } else {
                error_json('提交错误');
            }
        }
    }
    function refund_kd_submit()
    {
        if ($_POST) {
            $service_id = isset($_POST['service_id']) ? $_POST['service_id'] : error_json('提交错误：SERVICE ID NULL');
            $kd_code = isset($_POST['kd_code']) ? $_POST['kd_code'] : error_json('快递单号未填写');
            $kd_number = isset($_POST['kd_number']) ? $_POST['kd_number'] : error_json('快递单号未填写');
            $kd_remark = isset($_POST['kd_remark']) ? $_POST['kd_remark'] : '';
            $refund_img = isset($_POST['refund_img']) ? $_POST['refund_img'] : '';
            $row = $this->db->get_one("select * from " . table('refund') . " where uid = " . $this->uid . " and service_id = '" . $service_id . "'");
            if ($row) {
                if ($row['id']) {
                    $this->db->autocommit();
                    $kd_row = $this->module('express')->get_config($kd_code, 'code');
                    $con = '';
                    $con .= '物流公司：' . $kd_row['name'] . '<br>';
                    $con .= '退货单号：' . $kd_number . ' <i class="kd_info" data-code="' . $kd_number . '" data-name="' . $kd_row['name'] . '"></i><br>';
                    if (!empty($kd_remark)) {
                        $con .= '退货备注：' . $kd_remark . '<br>';
                    }
                    if ($refund_img) {
                        foreach ($refund_img as $v) {
                            $img_arr[] = $v;
                        }
                        $logarr['images'] = serialize($img_arr);
                    }
                    $logarr['rid'] = $row['id'];
                    $logarr['title'] = '买家：已经退货';
                    $logarr['content'] = $con;
                    $log_query = $this->module('refund')->add_process($logarr);
                    $query = $this->db->query("update " . table('refund') . " set status = 3 where id = " . $row['id']);
                    if ($log_query && $query) {
                        $this->db->commit();
                        $data['error'] = '0';
                        echo json_encode($data);
                    } else {
                        $this->db->rollback();
                        error_json('提交失败');
                    }
                } else {
                    error_json('退货不存在');
                }
            } else {
                error_json('系统繁忙');
            }
        } else {
            error_json('提交错误');
        }
    }
}